package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Genealogy;
import com.example.demo.service.GenealogyService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/genealogy")
public class GenealogyController {

    @Resource
    private GenealogyService genealogyService;

    // 增添数据
    @PostMapping("/add")
    public Result add(@RequestBody Genealogy genealogy) {
        genealogyService.add(genealogy);
        return Result.success();
    }

    // 更新数据
    @PutMapping("/update")
    public Result update(@RequestBody Genealogy genealogy) {
        genealogyService.update(genealogy);
        return Result.success();
    }

    // 删除单个数据
    @DeleteMapping("/deleteById/{genealogyId}")
    public Result deleteById(@PathVariable Long genealogyId) {
        genealogyService.deleteById(genealogyId);
        return Result.success();
    }

    // 查询所有数据
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Genealogy> list = genealogyService.selectAll();
        return Result.success(list);
    }

    // 查询单个数据
    @GetMapping("/selectById/{genealogyId}")
    public Result selectById(@PathVariable Long genealogyId) {
        Genealogy genealogy = genealogyService.selectById(genealogyId);
        return Result.success(genealogy);
    }

    // 分页查询
    // pageNum: 当前页码
    // pageSize: 每页的个数
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "9") Integer pageSize) {
        PageInfo<Genealogy> pageInfo = genealogyService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }
}